﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace dzs.Web.supply
{
    public partial class ListTotalMonthSupply : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                BindDDL();
                Bind();

            }
        }


        private void BindDDL()
        {
            string str = " roles_id = 6 ";

            DataSet ds = new dzs.BLL.user().GetList(str);

            this.ddl_gygly.DataSource = ds;
            this.ddl_gygly.DataTextField = "user_name";
            this.ddl_gygly.DataValueField = "user_id";
            this.ddl_gygly.DataBind();

        }

        private void Bind()
        {
            Maticsoft.BLL.monthSupplayPlan msm = new Maticsoft.BLL.monthSupplayPlan();
            Maticsoft.BLL.supplyPlan sm = new Maticsoft.BLL.supplyPlan ();

            List<dzs.Model.configinfo> cm = new dzs.BLL.configinfo().GetModelList("");//获取时间结算日期
            dzs.Model.configinfo c = cm[0];

            string user_id = this.ddl_gygly.SelectedItem.Value;
            string str = " user_id = " + user_id;

            if (!string.IsNullOrEmpty(this.txt_supplyDate.Text))
            {
                DateTime ghq = DateTime.Parse(this.txt_supplyDate.Text);
                #region 月供应计划起始结束时间
               
                DateTime monthEndTime;//本月供应计划结束时间
                DateTime monthStartTime;//本月供应计划起始时间

                
                int day = ghq.Day;

                if (day <= c.ygyjs)
                {
                    monthStartTime = DateTime.Parse(ghq.AddMonths(-1).ToString("yyyy-MM-" + c.ygyqs + ""));
                    monthEndTime = DateTime.Parse(ghq.ToString("yyyy-MM-" + c.ygyjs + ""));
                }
                else
                {
                    monthStartTime = DateTime.Parse(ghq.ToString("yyyy-MM-" + c.ygyqs + ""));
                    monthEndTime = DateTime.Parse(ghq.AddMonths(1).ToString("yyyy-MM-" + c.ygyjs + ""));
                }
                #endregion

                str += " and supplyDate between '" + monthStartTime + "' and '" + monthEndTime + "'  ";//此处的supplyDate为supplyPlan中的供货期，不是供应年月
            }

            DataSet ds_monthSupply = msm.GetListMonthSupplyTotalCount(str);
            DataSet ds_supply = sm.GetList(str);

            int monthSupplyCount = 0;//生成的月供应计划数量
            int supplyCount = 0;//供应计划总数
            decimal present = 0;//当月计划生成及时率

            if (ds_monthSupply.Tables[0].Rows.Count > 0)
            {
                monthSupplyCount = ds_monthSupply.Tables[0].Rows.Count;
            }
            if (ds_supply.Tables[0].Rows.Count > 0)
            {
                supplyCount = ds_supply.Tables[0].Rows.Count;
            }

            if (supplyCount != 0)
            {
                present = decimal.Divide(monthSupplyCount, supplyCount);
                present = decimal.Round(present, 2)*100;
                this.lbl_complate.Text = "已生成的月供应计划数：" + monthSupplyCount + "；当月供应计划总数：" + supplyCount + "；供应计划完成率为：" + present + "%。";
            }

            this.GridView1.DataSource = ds_monthSupply;
            this.GridView1.DataBind();

            this.lbl_supplyDateWarn.Text = "*月供应计划起始日期为上月 " + c.ygyqs + " 号至当月 " + c.ygyjs + " 号";
        }

        protected void btn_sel_Click(object sender, EventArgs e)
        {
            Bind();
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.GridView1.PageIndex = e.NewPageIndex;

            Bind();
        }

        //导出复写
        public override void VerifyRenderingInServerForm(Control control)       //导出
        { }
        //导出excel
        protected void btn_out_Click(object sender, EventArgs e)
        {
            this.GridView1.AllowPaging = false;

            Bind();

            if (this.GridView1.Rows.Count != 0)
            {
                Response.Clear();
                Response.Buffer = true;
                Response.Charset = "GB2312";
                Response.AppendHeader("Content-Disposition", "attachment;filename=yuegongjihua.xls");
                //Response.ContentEncoding = System.Text.Encoding.UTF7;
                Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
                Response.ContentType = "application/ms-excel";
                System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
                System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
                Response.Output.Write("<H4 align='center'>当月供应计划生成及时率</H4>");
                this.GridView1.RenderControl(oHtmlTextWriter);
                Response.Output.Write(oStringWriter.ToString());
                Response.Flush();
                Response.End();

            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('数据为空，无法导出表格')</script>");

            }

        }
    }
}